using System;
using System.Threading;
using System.Windows.Forms;
using Preparation;

namespace Tester.GuiActions.Actions.Impl
{
    public class SaveFileAction : BaseAction
    {
        public override bool Visible
        {
            get
            {
                return TesterMultyDocument.Instance != null && 
                      TesterMultyDocument.Instance.Suspended == TesterMultyDocument.SuspendStates.Stopped &&
                      TesterMainForm.Instance.TopControl == EmbededControlTags.TesterList;
            }
        }

        public override void CustomActionForMenu(MainMenuManager.MainMenuItem item)
        {
            TesterMainForm.Instance.FormClosing -= MainActionOfClosing;
            TesterMainForm.Instance.FormClosing += MainActionOfClosing;
        }

        private void MainActionOfClosing(object sender, FormClosingEventArgs e)
        {
            if (TesterMultyDocument.Instance != null &&
                TesterMultyDocument.Instance.Suspended != TesterMultyDocument.SuspendStates.Stopped)
            {
                DialogResult result = MessageBox.Show("Stop translation!", "Document is translating");
                e.Cancel = true;
                return;
            }
            MainAction(sender, e);
        }

        public override void MainAction(object sender, EventArgs e)
        {
            if (Visible)
            {
                TesterList.Instance.Save(false);
                MessageBox.Show("Operation complete.", "Saved.");
            }
        }

    }
}
